grid = [
    ["1", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", "1", "2", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", "2", " ", "1", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", "2", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
    [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", ],
]


def near_pos(grid, start_x, ignore_list=[0, 1]):
    """
    距离x最近的 坐标
    :param grid: 矩阵
    :param start_x: 横起始坐标
    :param ignore_list: 忽略数字列表
    :return:
    """
    result = []
    for row in grid:
        _r = []
        for index in range(start_x, len(row)):
            x = row[index]
            if x not in ignore_list:
                _r += [index]
        result += [_r]
    return result


def transform_matrix_x_y(grid):
    """
    矩阵x和y相互转换
    :param grid:
    :return:
    """
    return list(map(list, zip(*grid)))


def same_line(grid, same_length=3, _type="h", ignore_list=[0, 1]):
    """
    矩阵相同数字的坐标

    :param grid: 矩阵
    :param same_length: 相同数字的长度:
    :param _type: 'v' 纵向 'h' 横向
    :param ignore_list: 忽略的数字
    :return:
    """
    result = []

    if _type == 'v':
        same_length += 1
        grid = transform_matrix_x_y(grid)

    for row in grid:
        _result = []
        cache_list = []
        cache_position = []
        for index, x in enumerate(row):
            if not cache_list:
                cache_list += [x]
            if index > len(row) - 1:
                cache_list = []
            else:
                if x in ignore_list:
                    cache_list = []
                if cache_list and x == cache_list[0]:
                    cache_list += [x]
                    cache_position += [index]
                else:
                    cache_position = [index]
                    cache_list = [x]
            if len(cache_list) >= same_length:
                if cache_position in _result:
                    _result.remove(cache_position)
                _result += [cache_position]
        result += [_result]
    return result


def near(grid, target_position, r=3, self=False, loop=False):
    result = []
    _result = []
    x = target_position[0]
    y = target_position[1]
    z = 0
    for _y in range(r):
        for _z in range(z):
            _result += [[x + _z + 1, y + _y - r]]
            _result += [[x - _z - 1, y + _y - r]]

            _result += [[x + _z + 1, y - _y + r]]
            _result += [[x - _z - 1, y - _y + r]]
        z += 1
    for _x in range(r):
        _result += [[x - _x - 1, y]]
        _result += [[x + _x + 1, y]]
        _result += [[x, y - _x - 1]]
        _result += [[x, y + _x + 1]]
    if self: _result += [[x, y]]

    if not loop:
        _len = len(grid)
        for item in _result:
            if (item[0] >= 0) and (item[1] >= 0) and (item[0] < _len) and (item[1] < _len):
                result += [item]
    else:
        result = _result
    return result


def near_direction(grid, target_position, direction, r=3, loop=False):
    result = []
    _result = []
    x = target_position[0]
    y = target_position[1]
    z = 0
    if direction == 'topleft':
        for _y in range(r):
            for _z in range(z):
                _result += [[x - _z - 1, y + _y - r]]
            z += 1
        for _x in range(r):
            _result += [[x - _x - 1, y]]
            _result += [[x, y - _x - 1]]
    elif direction == 'topright':
        for _y in range(r):
            for _z in range(z):
                _result += [[x + _z + 1, y + _y - r]]
            z += 1
        for _x in range(r):
            _result += [[x + _x + 1, y]]
            _result += [[x, y - _x - 1]]
    elif direction == 'bottomleft':
        for _y in range(r):
            for _z in range(z):
                _result += [[x - _z - 1, y - _y + r]]
            z += 1
        for _x in range(r):
            _result += [[x - _x - 1, y]]
            _result += [[x, y + _x + 1]]
    elif direction == 'bottomright':
        for _y in range(r):
            for _z in range(z):
                _result += [[x + _z + 1, y - _y + r]]
            z += 1
        for _x in range(r):
            _result += [[x + _x + 1, y]]
            _result += [[x, y + _x + 1]]
    elif direction == 'top':
        for _y in range(r):
            for _z in range(r - z + 1, 0, -1):
                _result += [[x + _z - 1, y + _y - r]]
                _result += [[x - _z + 1, y + _y - r]]
            z += 1
    elif direction == 'bottom':
        for _y in range(r):
            for _z in range(r - z + 1, 0, -1):
                _result += [[x + _z - 1, y - _y + r]]
                _result += [[x - _z + 1, y - _y + r]]
            z += 1
    elif direction == 'right':
        for _y in range(r + 1):
            for _z in range(r - z + 1, 0, -1):
                _result += [[r + x - _z + 1, y - _y]]
                _result += [[r + x - _z + 1, y + _y]]
            z += 1
    elif direction == 'left':
        for _y in range(r + 1):
            for _z in range(r - z + 1, 0, -1):
                _result += [[x + _z - r - 1, y - _y]]
                _result += [[x + _z - r - 1, y + _y]]
            z += 1
    _result += [[x, y]]
    if not loop:
        _len = len(grid)
        for item in _result:
            if (item[0] >= 0) and (item[1] >= 0) and (item[0] < _len) and (item[1] < _len):
                result += [item]
    else:
        result = _result

    def unique(one_list):
        temp_list = []
        for one in one_list:
            if one not in temp_list:
                temp_list.append(one)
        return temp_list

    return unique(result)


def near_round(grid, target_list=[-1], r=2, ignore_list: list = [], loop=False):
    result = []

    def _near(position):
        _result = []
        x = position[0]
        y = position[1]
        for _x in range(r, 0, -1):
            for _y in range(r, 0, -1):
                _result += [[x - _x, y - _y]]
                _result += [[x - _x, y + _y]]
                _result += [[x + _x, y + _y]]
                _result += [[x + _x, y - _y]]
        for _x in range(r):
            _result += [[x - _x - 1, y]]
            _result += [[x + _x + 1, y]]
            _result += [[x, y - _x - 1]]
            _result += [[x, y + _x + 1]]
        return _result

    _list = []
    for y, row in enumerate(grid):
        for x, item in enumerate(row):
            if item in target_list:
                if [x, y] not in _list:
                    _list += [[x, y]]
                if item not in ignore_list:
                    result += _near([x, y])
    _result = []
    for item in result:
        if item not in _result:
            _result += [item]
    result = _result
    for item in _list:
        if item in result:
            result.remove(item)

    _result = []
    if not loop:
        _len = len(grid)
        for item in result:
            if (item[0] >= 0) and (item[1] >= 0) and (item[0] < _len) and (item[1] < _len):
                _result += [item]
        result = _result
    return result


# s1 = same_line(grid)
# s2 = same_line(grid, _type="v")
# lst = near(grid, [1, 1], 3)

# lst = near_direction(grid, [3, 3], "bottomleft", 2)
lst = near_round(grid, ['1', '2'], 1)

for x in lst:
    grid[x[1]][x[0]] = "*"

for x in grid:
    print(x)
